﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;
using MQ.Common;
using RabbitMQ.Client.Events;

namespace MQ.Consumer
{
    /// <summary>
    ///  消费消息-扇形交换机
    /// </summary>
    public class Receive_FanoutExchange
    {
        public static void Go()
        {
            //创建连接工厂
            var conn = RabbitMQHelper.GetConnection();
            {
                //创建信道
                var channel = conn.CreateModel();
                {
                    channel.ExchangeDeclare("ex_fanout_km", ExchangeType.Fanout);

                    //定义3个队列
                    string queueName1 = "queue_fanout_name1";
                    string queueName2 = "queue_fanout_name2";
                    string queueName3 = "queue_fanout_name3";
                    channel.QueueDeclare(queueName1, false, false, false, null);
                    channel.QueueDeclare(queueName2, false, false, false, null);
                    channel.QueueDeclare(queueName3, false, false, false, null);
                    //将队列绑定到交换机
                    channel.QueueBind(queue: queueName1, exchange: "ex_fanout_km", routingKey: "");//队列名，交换机名，路由键
                    channel.QueueBind(queue: queueName2, exchange: "ex_fanout_km", routingKey: "");//队列名，交换机名，路由键
                    channel.QueueBind(queue: queueName3, exchange: "ex_fanout_km", routingKey: "");//队列名，交换机名，路由键
                    
                    var task1 = Task.Run(() =>
                    {
                        //定义消费者，接收消息事件
                        var consumer1 = new EventingBasicConsumer(channel);
                        consumer1.Received += (send, e) =>
                        {
                            var msg = Encoding.UTF8.GetString(e.Body.ToArray());
                            Console.WriteLine($" 消费者1 接收到消息：{msg}");
                        };
                        //将消费者绑定到指定队列
                        channel.BasicConsume(queueName1, true, consumer1);
                    });

                    var task2 = Task.Run(() =>
                    {
                        //定义消费者，接收消息事件
                        var consumer2 = new EventingBasicConsumer(channel);
                        consumer2.Received += (send, e) =>
                        {
                            var msg = Encoding.UTF8.GetString(e.Body.ToArray());
                            Console.WriteLine($" 消费者2 接收到消息：{msg}");
                        };
                        //将消费者绑定到指定队列
                        channel.BasicConsume(queueName2, true, consumer2);
                    });
                    var task3 = Task.Run(() =>
                    {
                        //定义消费者，接收消息事件
                        var consumer3 = new EventingBasicConsumer(channel);
                        consumer3.Received += (send, e) =>
                        {
                            var msg = Encoding.UTF8.GetString(e.Body.ToArray());
                            Console.WriteLine($" 消费者3 接收到消息：{msg}");
                        };
                        //将消费者绑定到指定队列
                        channel.BasicConsume(queueName3, true, consumer3);
                    });
                    Task.WaitAll(task1, task2, task3);
                }
            }
        }
    }
}
